﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BusinessEntities;
using DataAccessLayer;
using Draco.Algorithms;
using Draco.DataStructure;

namespace BusinessLayer
{
    public class GrogshopBLL:LocatingPointBLL<Grogshop>
    {
        private D_LinkList<Grogshop> grogshops;

        public D_LinkList<Grogshop> Grogshops { get { return grogshops; } }

        public GrogshopBLL()
        {
            GrogshopDAL dal = new GrogshopDAL();
            grogshops = dal.GetElements();
        }

        #region 查找附近酒店

        /// <summary>
        /// 查找指定坐标附近的酒店
        /// </summary>
        /// <param name="x">坐标x</param>
        /// <param name="y">坐标y</param>
        /// <param name="errorLevel">允许误差范围</param>
        /// <returns>酒店列表</returns>
        public override D_LinkList<Grogshop> GetNearbySites(int x, int y, SeekAdmissibleError errorLevel)
        {
            int hashLen = 8;
            switch (errorLevel)
            {
                case SeekAdmissibleError.Level_1:
                    hashLen = 8;
                    break;
                case SeekAdmissibleError.Level_2:
                    hashLen = 7;
                    break;
                case SeekAdmissibleError.Level_3:
                    hashLen = 6;
                    break;
                case SeekAdmissibleError.Level_4:
                    hashLen = 5;
                    break;
                case SeekAdmissibleError.Level_5:
                    hashLen = 4;
                    break;
                case SeekAdmissibleError.Level_6:
                    hashLen = 3;
                    break;
                case SeekAdmissibleError.Level_7:
                    hashLen = 2;
                    break;
                case SeekAdmissibleError.Level_8:
                    hashLen = 1;
                    break;
            }
            D_GeoHash geo = new D_GeoHash(x, y);
            geo.SetHashLenght(hashLen);
            return GetNearbyGrogshops(geo.GetGeoHashBase4For9());
        }

        private D_LinkList<Grogshop> GetNearbyGrogshops(string geohash)
        {
            D_LinkList<Grogshop> nearby = new D_LinkList<Grogshop>();
            foreach (Grogshop grogshop in Grogshops)
            {
                if (D_String.PrefixMatching(grogshop.GeoHash, geohash))
                {
                    nearby.Add(grogshop);
                }
            }
            return nearby;
        }

        private D_LinkList<Grogshop> GetNearbyGrogshops(D_LinkList<string> geohashs)
        {
            D_LinkList<Grogshop> nearby = new D_LinkList<Grogshop>();
            foreach (Grogshop grogshop in Grogshops)
            {
                foreach (string geohash in geohashs)
                {
                    if (D_String.PrefixMatching(grogshop.GeoHash, geohash))
                    {
                        nearby.Add(grogshop);
                    }
                }
            }
            return nearby;
        }

        #endregion


        public override Grogshop GetSite(int id)
        {
            foreach (Grogshop grogshop in Grogshops)
            {
                if (grogshop.ID == id)
                {
                    return grogshop;
                }
            }
            return null;
        }

        public override void AddSite(Grogshop site)
        {
            //id从数据库获得
            GrogshopDAL dal = new GrogshopDAL();
            dal.Insert(site.Name,site.X,site.Y,site.Grade);
            int id = dal.GetLastInsertID();

            Grogshop temp = new Grogshop(id,site.Name,site.X,site.Y,site.Grade);
            Grogshops.Add(temp);
        }

        public override void UpdateSite(Grogshop site)
        {
            for (int i = 0; i < Grogshops.Count; i++)
            {
                if (Grogshops[i].ID == site.ID)
                {
                    Grogshops[i] = site;
                }
            }
            //更新数据库
            GrogshopDAL dal = new GrogshopDAL();
            dal.Update(site.ID,site.Name,site.X,site.Y,site.Grade);
        }

        public override void DeleteSite(int id)
        {
            int[] array = new int[Grogshops.Count];
            for (int i = 0; i < Grogshops.Count; i++)
            {
                array[i] = Grogshops[i].ID;
            }

            int index = D_Search.BinarySearchIndex(array, id);

            Grogshops.RemoveAt(index);

            //更新数据库
            GrogshopDAL dal = new GrogshopDAL();
            dal.Delete(id);
        }


    }
}
